package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.User;
import common.BaseServlet;
import service.adminMemberDAO;

/**
 * Servlet implementation class AdminMember
 */
@WebServlet("/AdminMember")
public class AdminMember extends BaseServlet {
	private static final long serialVersionUID = 1L;
	 private final int pagesize=2;//分页的单页数据条数
	 adminMemberDAO amDAO=new adminMemberDAO();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminMember() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	public String basefun(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");

		PrintWriter out;
		out = response.getWriter();
		//获取页数
		String viewStr=request.getParameter("viewPage");
		int viewPage=(viewStr==null)?1:Integer.parseInt(viewStr);
		request.setAttribute("currentPage", viewPage);
    	out.println("viewPage:"+viewPage);
    	List<User> list=new ArrayList<User>();
    	//获取总会员数据
		list=amDAO.queryAllMembers(pagesize,viewPage);
		System.out.println(list.size());
		//获取会员数据总条数
		int listCount=amDAO.listAll();
		request.setAttribute("listCount", listCount);
		HttpSession session=request.getSession();
		session.setAttribute("list", list); 
		
		StringBuffer itemsList=new StringBuffer();
		itemsList.append("[");
		for(int i=0;i<list.size();i++) {
			User user=list.get(i);
			itemsList.append("{\"userid\":\""+user.getUserid()+"\",");
			itemsList.append("\"nickname\":\""+user.getNickname()+"\",");
			itemsList.append("\"name\":\""+user.getName()+"\",");
			itemsList.append("\"level\":\""+user.getLevel()+"\",");
			itemsList.append("\"sex\":\""+user.getSex()+"\",");
			itemsList.append("\"points\":\""+user.getPoints()+"\",");
			itemsList.append("\"register_time\":\""+user.getRegister_time()+"\",");
			itemsList.append("\"tel\":\""+user.getTel()+"\",");
			System.out.println(user.getRegister_time());
			if (i==list.size()-1) {
				itemsList.append("\"ifexist\":\""+user.getIfexist()+"\"}");
			}
			else {
				itemsList.append("\"ifexist\":\""+user.getIfexist()+"\"},");
			}
		}
		itemsList.append("]");
		request.setAttribute("items",new String(itemsList));
		
		
		return "f:htjsp/MemberList";
	}
	//会员批量删除（伪）方法
			public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException{
				PrintWriter out;
				out=response.getWriter();
				String str=request.getParameter("str");
				System.out.println(str);
				if(str.equals("")) {
					out.print("fail");
				}
				else {
				String[] userid=str.split(",");
				for(int i=0;i<userid.length;i++) {
					amDAO.delete(userid[i]);
				}
				out.print("ok");
				}
			}
			public String addUser(HttpServletRequest request, HttpServletResponse response) throws IOException{
				response.setCharacterEncoding("UTF-8");
				request.setCharacterEncoding("UTF-8");
				PrintWriter out;
				out = response.getWriter();
				int userid= Integer.parseInt(request.getParameter("userid"));
				System.out.println(userid);
				List<User> list=new ArrayList<User>();
				list=amDAO.queryMembers(userid);
				HttpSession session=request.getSession();
				session.setAttribute("list", list); 
				return "f:htjsp/AddMember";
			}
			
			public void UserEdit(HttpServletRequest request, HttpServletResponse response) throws IOException{
				response.setCharacterEncoding("UTF-8");
				request.setCharacterEncoding("UTF-8");
				PrintWriter out;
				out = response.getWriter();
				String nickname=request.getParameter("nikename");
				int userid= Integer.parseInt(request.getParameter("userid"));
				int sex= Integer.parseInt(request.getParameter("sex"));
				int exp= Integer.parseInt(request.getParameter("exp"));
				int points= Integer.parseInt(request.getParameter("points"));
				int ifexist= Integer.parseInt(request.getParameter("ifexist"));
				
				amDAO.updateUser(userid, sex, points, exp, ifexist, nickname);
				
				out.print("ok");
			}
			
			
			
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	

}
